package com.iseastar.guojiang.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import droid.frame.utils.lang.Str;

@SuppressLint("SimpleDateFormat")
public class HandlerTime {

    public static final long MIN = 60 * 1000L;

    public static final long HOUR = 60 * MIN;

    public static final long DAY = 24 * HOUR;

    public static final long MONTH_HARF = 15 * DAY;

    public HandlerTime() {

    }

    public Date obtainCurrentTime() {
        Date currentDate = new Date(System.currentTimeMillis());
        return currentDate;
    }

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    // public String getBeforeTimeString(Date date) {
    public String getBeforeTimeString(long happenTime) {
        long now = System.currentTimeMillis();
        // long happenTime = date.getTime();
        long passed = now - happenTime;
        if (passed <= MIN) {
            return "刚刚";
        }
        if (passed <= HOUR) {
            int mins = (int) (passed / MIN);
            return mins + "分钟前";
        }
        if (passed <= DAY) {
            int hours = (int) (passed / HOUR);
            return hours + "小时前";
        }
        //
        if (passed <= MONTH_HARF) {
            int days = (int) (passed / DAY);
            return days + "天前";
        } else {// 超过15天显示时间 (年月日)
            return dateFormat.format(new Date(happenTime));
        }
    }

    public String conversion(long time) {
        if (time == 0 || time < 0) {
            return "0";
        }
        Date currentDate = new Date(time);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
        String dateString = dateFormat.format(currentDate);
        return dateString;
    }

    public String circleCurrentTime() {
        obtainCurrentTime();
        Date currentDate = obtainCurrentTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
        String dateString = dateFormat.format(currentDate);
        return dateString;
    }

    public String obtainSignTime() {
        Date currentTime = obtainCurrentTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyMMddhhmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public int obtainCurrentHours(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH");
        String dateString = formatter.format(currentTime);
        try {
            int hour = Integer.valueOf(dateString);
            return hour;
        } catch (Exception e) {
            return 8;
        }
    }

    public String obtainCurrentHHMM() {
        Date currentTime = obtainCurrentTime();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String HHMM = formatter.format(currentTime);
        return HHMM;
    }

    public int obtainCurrentHours(String currentTime) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

            Date d = null;
            try {
                d = dateFormat.parse(currentTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            SimpleDateFormat formatter = new SimpleDateFormat("HH");
            String dateString = formatter.format(d);

            int hour = Integer.valueOf(dateString);
            return hour;
        } catch (Exception e) {
            return 8;
        }
    }

    public String obtainCurrentYMDHMS(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    //
    // SimpleDateFormat dateFormat = new SimpleDateFormat(
    // "yyyy-MM-dd'T'hh:mm:ss");
    //
    // Date d = null;
    // try {
    // d = dateFormat.parse(date);
    // } catch (ParseException e) {
    // e.printStackTrace();
    // }

    /**
     * 输入时间戳，返回时间段
     *
     * @param time
     */
    public static String getTime(long time) {
        // SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        // mSimpleDateFormat = new SimpleDateFormat("HH:mm");
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("HH:mm");
        if (mSimpleDateFormat != null) {
            String hhmm = mSimpleDateFormat.format(new Date(time));
            String[] hourMinute = hhmm.split(":");
            if (hourMinute.length > 1) {
                String hourString = hourMinute[0];
                String minuteString = hourMinute[1];
                int hourInt = Integer.parseInt(hourString);
                int hourInt2 = hourInt + 2;
                String hm = hourInt2 + ":" + minuteString;
                String time2 = hhmm + "-" + hm;
                return time2;
            }
        }
        return "";
    }

    /**
     * ***********注意该方法不能在getView中调用，性能低劣**************** <br>
     * 输入时间戳，返回时间段
     *
     * @param timeFrom
     */
    public static String getTime(long timeFrom, long timeTo) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("HH:mm");
        String hhmm1 = mSimpleDateFormat.format(new Date(timeFrom));
        String hhmm2 = mSimpleDateFormat.format(new Date(timeTo));
        String hm = hhmm1 + "-" + hhmm2;
        return hm;
    }

    /**
     * 返回 一月  二月
     *
     * @param month 传入 06或者6
     * @return
     */
    private static String[] monthStr = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};

    public static String monthStr(String month) {
        if (Str.isEmpty(month)) {
            return "";
        }
        try {
            if (month.startsWith("0")) {
                int monthIndex = Integer.parseInt(month.substring(1));
                return monthStr[monthIndex - 1];
            } else {
                int monthIndex = Integer.parseInt(month);
                return monthStr[monthIndex - 1];
            }
        } catch (Exception e) {
        }
        return "";
    }
}
